#ifndef __TINY_TEST__

#include "vbb_GSM_CNS03.h"
#include "fw/vbb_RTC.h"
#include "vbb_GSM_Communicator.h"
#include <stdio.h>

extern VBB_Pin PWRKEY_Pin;
extern VBB_Pin STATUS_Pin;

void GSM_SwitchOff_CNS03(void)
{
    vbb_PIN_Set(&PWRKEY_Pin, TRUE);
    vbb_DelayMillis(1000);
    vbb_PIN_Set(&PWRKEY_Pin, FALSE);
}

void GSM_SwitchOn_CNS03(void)
{
    vbb_PIN_Set(&PWRKEY_Pin, TRUE);
    vbb_DelayMillis(1000);
    vbb_PIN_Set(&PWRKEY_Pin, FALSE);
}

bool GSM_IsOn_CNS03(void)
{
    return (vbb_PIN_Get(&STATUS_Pin) == 1);
}

bool GSM_IsOff_CNS03(void)
{
    return (vbb_PIN_Get(&STATUS_Pin) == 0);
}

extern bool _ba01_noNeedATFCmd;

bool GSM_Ready_CNS03(void)
{
    if (!_ba01_noNeedATFCmd)
    {
        _ba01_noNeedATFCmd = TRUE;                
        return (GSM_Command("AT&F\r", "Call Ready", "+CFUN:", "+CPIN", NULL, 1) > 0);        
    }
    else
        return (GSM_Command("", "Call Ready", "+CFUN:", "+CPIN", NULL, 1) > 0);
}

void GSM_SetUp_CNS03(void)
{   
    vbb_OUTPUT_Print("INFO: GSM module settings up...");
    
    GSM_Command("ATE0\r", "OK", "ERROR", NULL, NULL, 5);        
    GSM_Command("AT+CSCLK=0\r", "OK", "ERROR", NULL, NULL, 5); //2013.09.25 Experiment Deactivate Sleep Mode                

    vbb_OUTPUT_Print("[DONE]\r\n");
}

extern char _gsm_Server[64];
extern char _gsm_ServerDoc[256];

extern char _gsm_tmpCmd[256];
extern char _gsm_tmpCmd2[256];

extern char _gsm_tmp[1024];
extern VBB_USART gsmUsart;

extern VBB_Message gsmMessage;

char GSM_GPRS_Open_CNS03(void)
{  
    if (GSM_Command("AT+CREG?\r", "+CREG: 0,1", "+CREG: 0,2", "+CREG: 0,3", "+CREG: 0,5", 5) == 1)
    {                
        if (!GSM_GPRS_IsOpen())
        {            
            GSM_PutCommand("AT+SAPBR=1,1\r");  
            return TRUE;
        }
        else
            return FALSE;
    }
    else
    {
        vbb_OUTPUT_Print("DEBUG: SIM card error.\r\n");
        return FALSE;
    }
}

char GSM_GPRS_WaitForOpen_CNS03(void)
{
    GSM_WaitForAnswerUpdate("OK", "ERROR", NULL, NULL, 120);
    if (GSM_CommandFinished())
    {
        if (GSM_GPRS_IsOpen())
            return GPRS_ATTACHED;
        else
            return GPRS_FAILED;
    }
    else
        return GPRS_ATTACHING;
}

bool _regDone;

char GSM_NetworkSet_CNS03(void)
{
    char ret = GSM_Command("AT+COPS?\r", "VIETTEL", "MOBIFONE", "VINAPHONE", "OK", 5);   
    if (ret == 4)
        ret = GSM_Command("AT+COPS?\r", "Viettel", "Mobifone", "Vinaphone", "OK", 5);    
    
    if (!_regDone)
    {
        GSM_Command("AT+CGPSPWR=1\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+CGPSIPR=9600\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+CGPSOUT=32\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+CGPSRST=1\r", "OK", "ERROR", NULL, NULL, 5);    
        _regDone = TRUE;
    }    
    
    GSM_Command("AT+CIPSHUT\r", "OK", "ERROR", NULL, NULL, 5);
    GSM_Command("AT+CIPMUX=0\r", "OK", "ERROR", NULL, NULL, 5);
    GSM_Command("AT+CIPQSEND=1\r", "OK", "ERROR", NULL, NULL, 5);
    //GSM_Command("AT+CIPRXGET=1\r", "OK", "ERROR", NULL, NULL, 5);
    GSM_Command("AT+SAPBR=0\r", "OK", "ERROR", NULL, NULL, 5);    
    GSM_Command("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r", "OK", "ERROR", NULL, NULL, 5);    
    
    if (ret == 1)
    {
        vbb_OUTPUT_Print("DEBUG: Use GPRS settings for Viettel network.\r\n");
        GSM_Command("AT+SAPBR=3,1,\"APN\",\"v-internet\"\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+SAPBR=3,1,\"USER\",\"\"\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+SAPBR=3,1,\"PWD\",\"\"\r", "OK", "ERROR", NULL, NULL, 5);
    }
    else if (ret == 2 || ret == 3)
    {
        vbb_OUTPUT_Print("DEBUG: Use GPRS settings for VNPT network.\r\n");
        GSM_Command("AT+SAPBR=3,1,\"APN\",\"m-wap\"\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+SAPBR=3,1,\"USER\",\"mms\"\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+SAPBR=3,1,\"PWD\",\"mms\"\r", "OK", "ERROR", NULL, NULL, 5);
    }
    else
    {
        vbb_OUTPUT_Print("DEBUG: Use GPRS settings for unknown network.\r\n");
        GSM_Command("AT+SAPBR=3,1,\"APN\",\"m-wap\"\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+SAPBR=3,1,\"USER\",\"mms\"\r", "OK", "ERROR", NULL, NULL, 5);
        GSM_Command("AT+SAPBR=3,1,\"PWD\",\"mms\"\r", "OK", "ERROR", NULL, NULL, 5);
    }    
    
    return ret;
}

bool GSM_GPRS_IsOpen_CNS03(void)
{
    return GSM_Command("AT+SAPBR=2,1\r", "+SAPBR: 1,1", "OK", "ERROR", NULL, 5) == 1;
}

extern char _serverIP[64];
extern char _serverPort[16];

bool GSM_TCP_IsOpen_CNS03(void)
{
    int ret = GSM_Command("AT+CIPSTATUS\r", "STATE: CONNECT OK", "OK", "ERROR", NULL, 5);
    if (ret == 1)    
        return TRUE;
    else
        return FALSE;
}

char GSM_TCP_Open_CNS03(const char * ServerAddr)
{
    GSM_Command("AT+CIPCLOSE\r", "OK", "ERROR", NULL, NULL, 5);
    sprintf(_gsm_tmpCmd, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r", _serverIP, _serverPort);
    GSM_PutCommand(_gsm_tmpCmd);                
    
    return TRUE;
}

char GSM_TCP_WaitForOpen_CNS03(void)
{
    int ret = GSM_WaitForAnswerUpdate("CONNECT OK", "CONNECT FAIL", NULL, NULL, 30);
    if (GSM_CommandFinished())
    {
        if (ret == 1)
            return TCP_CONNECTION_UP;
        else
            return TCP_CONNECTION_DOWN;
    }
    else
        return TCP_CONNECTION_CONNECTING;
}

char GSM_TCP_Opened_CNS03(const char * Document, char * DataBuffer, int * DataSize, char * TmpBuffer)
{
    GSM_BuildDataBuffer(Document, DataBuffer, DataSize, TmpBuffer);    
    return TRUE;
}

extern char * wait_command;
extern char * wait_value;

extern int wait_sendSize;

char GSM_TCP_DataWaitForReady_CNS03(void)
{    
    int ret = GSM_WaitForAnswerUpdate(">", "ERROR", NULL, NULL, 5);
    if (GSM_CommandFinished())
    {
        if (ret == 1)
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}

int GSM_TCP_DataPrepareSend_CNS03(char * DataBuffer, int DataSize)
{        
    sprintf(_gsm_tmpCmd, "AT+CIPSEND=%d\r", DataSize);
    GSM_PutCommand(_gsm_tmpCmd);    
    return DataSize;
}

int GSM_TCP_DataSendData_CNS03(char * DataBuffer, int SendSize)
{    
    int i;    
    for (i = 0; i < SendSize; i++)
    {
        GSM_WriteByte(DataBuffer[i]);    
        //vbb_OUTPUT_WriteByte(DataBuffer[i]);
    }
    
    i = GSM_Command("", "DATA ACCEPT", "ERROR", NULL, NULL, 10);
    if (i == 1)
        return SendSize;
    else
        return -1; 
}

char GSM_TCP_WaitForServerClosed_CNS03(void)
{    
    int ret = 0;
    //ret = GSM_Command("AT+CIPSTATUS\r", "STATE: TCP CLOSING", "STATE: TCP CLOSED", "OK", "ERROR", 5);
    ret = 2;
    vbb_CONSOLE_Printf("[%d]", ret);
    if (ret == 1 || ret == 2)
        return TCP_CONNECTION_DOWN;
    else 
        return TCP_CONNECTION_UP; 
}

char GSM_TCP_CloseClient_CNS03(void)
{
    int ret = GSM_Command("AT+CIPCLOSE=1\r", "OK", "ERROR", NULL, NULL, 10);
    return (ret == 1);    
}

int GSM_Communication_ReceivingData_CNS03(char * Buffer)
{
    int data_count = 0;
    int count;
    char * _tmp2;
    char * _tmp;
    int total_count = 0;
    
    vbb_GSM_Flush();
    //ret = GSM_Command("", "+IPD,", "CLOSED", NULL, NULL, 1);
    _tmp2 = gsmMessage.buff;    
    while (_tmp2)
    {
        vbb_GSM_Flush();
        _tmp2 = vbb_FindStr(_tmp2, "+IPD,");
        if (_tmp2)
        {
            _tmp2 += vbb_GetLen("+IPD,");
            sscanf(_tmp2, "%d", &data_count);
            vbb_CONSOLE_Printf("INFO: Received %d bytes of data.\r\n", data_count);
            
            if (data_count > 0)
            {
                _tmp = vbb_FindStr(_tmp2, ":");            
                while (_tmp == NULL)
                {
                    vbb_DelayMillis(20);
                    vbb_GSM_Flush();                
                    _tmp = vbb_FindStr(_tmp2, ":");
                }
                
                _tmp += vbb_GetLen(":");                                                
                
                while (vbb_GetLen(_tmp) < data_count)
                {
                    vbb_GSM_Flush();
                    vbb_DelayMillis(20);
                    //vbb_OUTPUT_Print("[!]");
                }
                
                count = data_count;
                while (count > 0)
                {                
                    Buffer[0] = _tmp[0];                
                    //vbb_OUTPUT_WriteByte(_tmp2[0]);
                    Buffer++;
                    _tmp++;
                    count--;
                }        
                
                total_count += data_count;
            }
        }
    }        
    
    if (total_count == 0)
    {
        _tmp2 = vbb_FindStr(gsmMessage.buff, "CLOSED");
        if (_tmp2)
            return -1;
        else
            return 0;
    }
    
    vbb_Message_Init(&gsmMessage);    
    return total_count;
}

#endif
